package com.qianying.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.qianying.entity.ShopInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 店铺信息数据访问层接口
 * 提供店铺信息的数据库操作方法，继承BaseMapper获得基础CRUD能力
 *
 * @author check
 * @date 2025-08-01
 */
@Mapper
public interface ShopInfoMapper extends BaseMapper<ShopInfo> {
    /**
     * 分页查询所有店铺（支持店铺名称模糊查询和状态筛选）
     * 关联查询客户、省市区、品牌信息
     *
     * @param offset   分页偏移量 (pageNum-1)*pageSize
     * @param pageSize 每页条数
     * @param shopName 店铺名称（可选，模糊匹配）
     * @param status   店铺状态（可选，精确匹配）
     * @return 分页数据列表（含关联信息）
     */
    @Select("<script>" +
            "SELECT SQL_CALC_FOUND_ROWS " +
            "si.*, " +
            "ci.customer_name AS customerName, " +
            "p.area_name AS provinceName, " +
            "c.area_name AS cityName, " +
            "d.area_name AS districtName, " +
            "cb.brand_name AS brandName " +
            "FROM shop_info si " +
            "LEFT JOIN customer_info ci ON si.customer_id = ci.id " +
            "LEFT JOIN china_area p ON si.province_id = p.id " +
            "LEFT JOIN china_area c ON si.city_id = c.id " +
            "LEFT JOIN china_area d ON si.district_id = d.id " +
            "LEFT JOIN custom_brand cb ON si.brand_id = cb.id " +
            "WHERE 1=1 " +
            // 店铺名称模糊查询（参数存在时才添加条件）
            "<if test='shopName != null and shopName != \"\"'>" +
            "AND si.shop_name LIKE CONCAT('%', #{shopName}, '%') " +
            "</if>" +
            // 状态筛选（参数存在时才添加条件）
            "<if test='status != null'>" +
            "AND si.status = #{status} " +
            "</if>" +
            "LIMIT #{offset}, #{pageSize};" +
            "</script>")
    List<ShopInfo> selectShopByPageWithCondition(
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize,
            @Param("shopName") String shopName,
            @Param("status") Integer status);
}
